package mblog.web.qiniu;

import com.qiniu.util.Auth;
import java.io.IOException;

import com.qiniu.common.Config;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;

public class UploadImg {
	// 设置好账号的ACCESS_KEY和SECRET_KEY
	static String ACCESS_KEY = "m3WFqHQGjR1PezWgOD9bBrOBHPh1RvApwrKId9VX";
	static String SECRET_KEY = "tefVyWqfCe5qdoJ0bLod_wuAZY7kXEVIHHCdAY0P";
	// 要上传的空间
	static String bucketname = "luosh";
	// 上传到七牛后保存的文件名
	static String key = "d4743637b8fbdb6a.png";
	// 上传文件的路径
	static String FilePath = "http://upload-images.jianshu.io/upload_images/1083963-d4743637b8fbdb6a.jpg";
	// 密钥配置
	Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
	// 创建上传对象
	BucketManager bucketManager = new BucketManager(auth);
	public void upload(String FilePath, String key) throws IOException {
		try {
			DefaultPutRet rs = bucketManager.fetch(FilePath, bucketname, key);
			// 打印返回的信息
			System.out.println(rs.hash + ":" + rs.key);
		} catch (QiniuException e) {
			Response r = e.response;
			// 请求失败时打印的异常的信息
			// {"hash":"FvoFul--zxXtinhF5FjK01XED-WR","key":"345b9f72-3919-4c4e-95a1-db625a1c35f7.png"}
			System.out.println(r.toString());
			try {
				// 响应的文本信息
				System.out.println(r.bodyString());
			} catch (QiniuException e1) {
				e1.printStackTrace();
			}
		}
	}

	public static void main(String args[]) throws IOException {
		new UploadImg().upload(FilePath, key);
	}

}